<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
class UserinfoController extends Controller
{
    //
    public function order()
    {   
        $uid = session('master') -> id;
        // dd($uid);
        $data = DB::table('orders') -> where('uid', $uid) -> get();
        // dd($data);

        foreach ($data as $key => $value) {
            
            // dd($value);
            $oid = $value -> oid;
            $addrid = $value -> addrid;
            $addr[$addrid] = DB::table('addr') -> where('id',$addrid) -> first();
            // dd( $addr[$addrid]);
            $data[$key] -> rec =  $addr[$addrid] -> rec; 
            $data[$key] -> tel =  $addr[$addrid] -> tel; 
            $data[$key] -> addr =  $addr[$addrid] -> addr; 


            $info[$oid] = DB::table('orders_info') 
            -> select('orders_info.*','goods.goodsname','goods.price')
            -> leftJoin('goods','orders_info.gid','=','goods.gid')
            -> where('oid','=',$oid)
            -> get();
            // dd($info[$oid]);
            $sum = 0;
            foreach ($info[$oid] as $key2 => $value2) {
                $info[$oid][$key2] -> total = $value2 -> scnt * $value2 -> price;
                $sum += $info[$oid][$key2] -> total;
            }
            $data[$key] -> sum = $sum;
        }
        // dd($data);
        return view('home.userinfo.order',['data' => $data]);

    }
    public function order_info($oid)
    {
        $order = DB::table('orders') -> where('oid',$oid) -> first();
        // dd($res);
        $addrid = $order -> addrid;
        $addr = DB::table('addr') -> where('id',$addrid) -> first();
        $info = DB::table('orders_info') 
        ->select('orders_info.*','goods.goodsname','goods.price','goodspic')
        ->leftJoin('goods','goods.gid','=','orders_info.gid')
        -> where('oid',$oid) 
        -> get();

        // dd($info);
        $sum = 0;
        foreach ($info as $key => $value) {
            $sum += $value -> scnt * $value -> price;
        }

        // dd($info);
        return view('home.userinfo.order_info',['addr' => $addr,'order'=> $order,'info' => $info,'sum' => $sum]);
    }

    public function delete(Request $request,$addrid)
    {

        $res = DB::table('addr') -> where('id', $addrid) -> delete();
        // dd($res);
        if($res)
        {
            return redirect('/home/userinfo') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    }

    public function update(Request $request)
    {
        $id = $request -> only('id');
        $data = $request -> except('_token','uid','id');
        $status = $request -> status;
        // dd($status);
        if($status)
        {
            DB::table('addr') ->where(['status' => $status]) -> update(['status' => '0']);
        }

       $res = DB::table('addr') ->where(['id' => $id]) -> update($data);

       if($res)
        {   
            if(session('addr_edit'))
            {       
                Session::put('addr_edit',false);
                return redirect('/home/order/index');

            }
             return redirect('/home/userinfo') -> with(['info' => '更新成功']);
           
        }else
        {
            return redirect('/home/userinfo') -> with(['info' => '更新失败']);
        }

    }

    public function edit(Request $request,$addrid)
    {   

        $data = DB::table('addr') -> where('id',$addrid) -> first();

        return view('home.userinfo.edit',['data' => $data]);
    }

    public function index(Request $request)
        {

            if($request -> isMethod('POST'))
            {
                $data = $request -> except('_token');
                // dd($data);
                $time = Date('Y-m-d H:i:s');
                $data['created_at'] = $time;
                $data['updated_at'] = $time;
                $status = $request -> status;
                // dd($status);
                if($status)
                {
                    
                    DB::table('addr') ->where(['status' => $status]) -> update(['status' => '0']);
                }

                $res = DB::table('addr') -> insert($data);
                if($res)
                {
                        if(session('addr_edit'))
                    {   
                        Session::forget('addr_edit');
                        return redirect('/home/order/index');
                    }

                    return redirect('/home/userinfo') -> with(['info' => '添加成功']);
                }else
                    {
                        if(session('addr_edit'))
                        {   
                            Session::put('addr_edit',false);
                            return redirect('/home/order/index');
                        }
                        return back() -> with(['info' => '更新失败']);
                    }

            }else
            {   

                $uid = session('master') -> id;
                $data = DB::table('addr') 
                -> select('addr.*','users.username')
                -> leftJoin('users','addr.uid','=','users.id')
                -> where('addr.uid','=',$uid)
                -> get();
            
             //     if(is_object($data)) {  
             //    $data = (array)$data;  
             // }
                // $data= json_decode($data);
             // dd($data);
            
            return view('home.userinfo.index',['title' => '个人中心','data' => $data]); 
            }
       }
         
 }

